package com.hualan.controller;

import com.hualan.pojo.Course;
import com.hualan.pojo.PageResult;
import com.hualan.pojo.Result;
import com.hualan.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result queryAllCourse(
            String title,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult pageResult=courseService.queryAllCourse(title,page,pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping
    public Result deleteCourse(@RequestParam List<Integer> ids){
        courseService.deleteCourse(ids);
        return Result.success();
    }

    @PostMapping
    public Result savecourse(@RequestBody Course course){
        courseService.saveCourse(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable Integer id){
        Course course=courseService.getCourseById(id);
        return Result.success(course);
    }

    @PutMapping
    public Result updateCourse(@RequestBody Course course){
        courseService.updateCourse(course);
        return Result.success();
    }
    @GetMapping("/find")
    public Result find(Integer id){
        List<Course> courses = courseService.find(id);
        return Result.success(courses);
    }
}
